﻿namespace HRM.CORE.REPORT
{
    using DevExpress.Utils;
    using DevExpress.XtraEditors;
    using DevExpress.XtraEditors.Controls;
    using DevExpress.XtraEditors.Repository;
    using DevExpress.XtraGrid;
    using DevExpress.XtraGrid.Columns;
    using DevExpress.XtraGrid.Views.Grid;
    using HRM.CORE;
    using Extra;
    using HRM.CORE.Properties;
    
    using System; using Extra.ERP;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Runtime.CompilerServices;
    using System.Windows.Forms; using Common;
    using DevExpress.XtraGrid.Views.Base;
    using APP.HELPER.Utils;

    public class xfmPrintPropertiesSelect : XtraForm
    {
        private SimpleButton bbiOK;
        private SimpleButton bbiPreview;
        private SimpleButton btClose;
        private GridColumn colFieldCode;
        private GridColumn colFieldName;
        private GridColumn colPosition;
        private GridColumn colSelect;
        private GridColumn colWidth;
        private IContainer components;
        private GridView gbList;
        private GridControl gcList;
        private LabelControl labelControl1;
        private int m_Index;
        private RepositoryItemImageComboBox repPosition;
        private string[] Title;
        private TextEdit txtTitle;
        private string[] xmlListFile;
        private string xmlListPath;
        private string[] xmlTitleFile;
        private string xmlTitlePath;

        public event AcceptedHandler Accepted;

        public event PrintedHandler Printed;

        public xfmPrintPropertiesSelect(int Index)
        {
            this.xmlListFile = new string[] { "employeeReportListOption.xml", "salaryReportListOption.xml" };
            this.xmlTitleFile = new string[] { "employeeReportTitleOption.xml", "salaryReportTitleOption.xml" };
            this.Title = new string[] { "DANH S\x00c1CH NH\x00c2N VI\x00caN", "BẢNG THANH TO\x00c1N LƯƠNG" };
            this.xmlListPath = "";
            this.xmlTitlePath = "";
            this.m_Index = 0;
            this.components = null;
            this.m_Index = Index;
            this.xmlListPath = Application.StartupPath + @"\Layout\" + this.xmlListFile[this.m_Index];
            this.xmlTitlePath = Application.StartupPath + @"\Layout\" + this.xmlTitleFile[this.m_Index];
        }

        public xfmPrintPropertiesSelect(int TypeDialog, int Index)
        {
            this.xmlListFile = new string[] { "employeeReportListOption.xml", "salaryReportListOption.xml" };
            this.xmlTitleFile = new string[] { "employeeReportTitleOption.xml", "salaryReportTitleOption.xml" };
            this.Title = new string[] { "DANH S\x00c1CH NH\x00c2N VI\x00caN", "BẢNG THANH TO\x00c1N LƯƠNG" };
            this.xmlListPath = "";
            this.xmlTitlePath = "";
            this.m_Index = 0;
            this.components = null;
            this.InitializeComponent();
            if (TypeDialog == 0)
            {
                this.bbiOK.Visible = false;
            }
            else if (TypeDialog == 1)
            {
                this.bbiPreview.Visible = false;
            }
            this.m_Index = Index;
            this.xmlListPath = Application.StartupPath + @"\Layout\" + this.xmlListFile[this.m_Index];
            this.xmlTitlePath = Application.StartupPath + @"\Layout\" + this.xmlTitleFile[this.m_Index];
            this.gcList.DataSource = this.LoadFullDataTable();
            this.txtTitle.Text = this.LoadTitle();
        }

        private void bbiOK_Click(object sender, EventArgs e)
        {
            this.SaveTitle();
            this.SaveSelectDataTable();
            this.RaiseAcceptedHandler();
            base.Close();
        }

        private void bbiPreview_Click(object sender, EventArgs e)
        {
            this.SaveTitle();
            this.RaisePrintedHandler(this.SaveSelectDataTable(), this.txtTitle.Text);
        }

        private void btClose_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private DataTable CreateNullDataTable()
        {
            DataTable table = new DataTable();
            DataColumn column = new DataColumn("Select", typeof(bool));
            DataColumn column2 = new DataColumn("FieldCode");
            DataColumn column3 = new DataColumn("FieldName");
            DataColumn column4 = new DataColumn("Width", typeof(float));
            DataColumn column5 = new DataColumn("Position", typeof(int));
            table.Columns.Add(column);
            table.Columns.Add(column2);
            table.Columns.Add(column3);
            table.Columns.Add(column4);
            table.Columns.Add(column5);
            return table;
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.gcList = new GridControl();
            this.gbList = new GridView();
            this.colSelect = new GridColumn();
            this.colFieldCode = new GridColumn();
            this.colFieldName = new GridColumn();
            this.colWidth = new GridColumn();
            this.colPosition = new GridColumn();
            this.repPosition = new RepositoryItemImageComboBox();
            this.labelControl1 = new LabelControl();
            this.bbiPreview = new SimpleButton();
            this.txtTitle = new TextEdit();
            this.btClose = new SimpleButton();
            this.bbiOK = new SimpleButton();
            this.gcList.BeginInit();
            this.gbList.BeginInit();
            this.repPosition.BeginInit();
            this.txtTitle.Properties.BeginInit();
            base.SuspendLayout();
            this.gcList.Location = new Point(8, 0x48);
            this.gcList.MainView = this.gbList;
            this.gcList.Name = "gcList";
            this.gcList.RepositoryItems.AddRange(new RepositoryItem[] { this.repPosition });
            this.gcList.Size = new Size(490, 0x108);
            this.gcList.TabIndex = 1;
            this.gcList.ViewCollection.AddRange(new BaseView[] { this.gbList });
            this.gbList.Appearance.HeaderPanel.Options.UseTextOptions = true;
            this.gbList.Appearance.HeaderPanel.TextOptions.HAlignment = HorzAlignment.Center;
            this.gbList.Appearance.HeaderPanel.TextOptions.WordWrap = WordWrap.Wrap;
            this.gbList.ColumnPanelRowHeight = 0x20;
            this.gbList.Columns.AddRange(new GridColumn[] { this.colSelect, this.colFieldCode, this.colFieldName, this.colWidth, this.colPosition });
            this.gbList.FixedLineWidth = 1;
            this.gbList.GridControl = this.gcList;
            this.gbList.Name = "gbList";
            this.gbList.OptionsCustomization.AllowFilter = false;
            this.gbList.OptionsCustomization.AllowSort = false;
            this.gbList.OptionsFilter.AllowFilterEditor = false;
            this.gbList.OptionsSelection.InvertSelection = true;
            this.gbList.OptionsView.EnableAppearanceEvenRow = true;
            this.gbList.OptionsView.NewItemRowPosition = NewItemRowPosition.Bottom;
            this.gbList.OptionsView.ShowAutoFilterRow = true;
            this.gbList.OptionsView.ShowGroupPanel = false;
            this.gbList.OptionsView.ShowIndicator = false;
            this.gbList.ViewCaption = "Đ\x00e1nh dấu  v\x00e0o trường dữ liệu bạn muốn in";
            this.colSelect.Caption = "Chọn";
            this.colSelect.FieldName = "Select";
            this.colSelect.Name = "colSelect";
            this.colSelect.Visible = true;
            this.colSelect.VisibleIndex = 0;
            this.colSelect.Width = 0x57;
            this.colFieldCode.Caption = "M\x00e3 trường";
            this.colFieldCode.FieldName = "FieldCode";
            this.colFieldCode.Name = "colFieldCode";
            this.colFieldName.Caption = "T\x00ean trường";
            this.colFieldName.FieldName = "FieldName";
            this.colFieldName.Name = "colFieldName";
            this.colFieldName.Visible = true;
            this.colFieldName.VisibleIndex = 1;
            this.colFieldName.Width = 0xcb;
            this.colWidth.Caption = "Độ rộng cột";
            this.colWidth.FieldName = "Width";
            this.colWidth.Name = "colWidth";
            this.colWidth.Visible = true;
            this.colWidth.VisibleIndex = 2;
            this.colWidth.Width = 0x60;
            this.colPosition.Caption = "Vị tr\x00ed";
            this.colPosition.ColumnEdit = this.repPosition;
            this.colPosition.FieldName = "Position";
            this.colPosition.Name = "colPosition";
            this.colPosition.Visible = true;
            this.colPosition.VisibleIndex = 3;
            this.colPosition.Width = 100;
            this.repPosition.AutoHeight = false;
            this.repPosition.Buttons.AddRange(new EditorButton[] { new EditorButton(ButtonPredefines.Combo) });
            this.repPosition.Items.AddRange(new ImageComboBoxItem[] { new ImageComboBoxItem("B\x00ean tr\x00e1i", 0, -1), new ImageComboBoxItem("Ở giữa", 1, -1), new ImageComboBoxItem("B\x00ean phải", 2, -1) });
            this.repPosition.Name = "repPosition";
            this.labelControl1.Appearance.Options.UseTextOptions = true;
            this.labelControl1.Appearance.TextOptions.WordWrap = WordWrap.Wrap;
            this.labelControl1.AutoSizeMode = LabelAutoSizeMode.None;
            this.labelControl1.Location = new Point(12, 11);
            this.labelControl1.Name = "labelControl1";
            this.labelControl1.Size = new Size(0x1e1, 0x1a);
            this.labelControl1.TabIndex = 3;
            this.labelControl1.Text = "  Nhập ti\x00eau đề cho b\x00e1o c\x00e1o v\x00e0o \x00f4 b\x00ean dưới, sau đ\x00f3 đ\x00e1nh dấu, nhập ti\x00eau đề, chỉnh độ rộng, chọn vị tr\x00ed hiển thị của cột v\x00e0o c\x00e1c trường cần hiển thị l\x00ean b\x00e1o c\x00e1o của bạn.";
            this.bbiPreview.Anchor = AnchorStyles.Bottom;
            this.bbiPreview.Image = MsgResc.filter;
            this.bbiPreview.Location = new Point(0x18a, 0x15f);
            this.bbiPreview.Name = "bbiPreview";
            this.bbiPreview.Size = new Size(0x68, 0x1b);
            this.bbiPreview.TabIndex = 5;
            this.bbiPreview.Text = "Xem trước";
            this.bbiPreview.Click += new EventHandler(this.bbiPreview_Click);
            this.txtTitle.EditValue = "DANH S\x00c1CH NH\x00c2N VI\x00caN";
            this.txtTitle.Location = new Point(8, 0x2e);
            this.txtTitle.Name = "txtTitle";
            this.txtTitle.Properties.Appearance.Font = new Font("Tahoma", 8.25f, FontStyle.Bold);
            this.txtTitle.Properties.Appearance.Options.UseFont = true;
            this.txtTitle.Size = new Size(490, 20);
            this.txtTitle.TabIndex = 2;
            this.txtTitle.TabStop = false;
            this.btClose.Anchor = AnchorStyles.Bottom;
            this.btClose.Image = Resources.Exit;
            this.btClose.Location = new Point(8, 0x15f);
            this.btClose.Name = "btClose";
            this.btClose.Size = new Size(0x68, 0x1b);
            this.btClose.TabIndex = 6;
            this.btClose.Text = "Đ\x00f3ng";
            this.btClose.Click += new EventHandler(this.btClose_Click);
            this.bbiOK.Anchor = AnchorStyles.Bottom;
            this.bbiOK.Image = MsgResc.check;
            this.bbiOK.Location = new Point(0x18a, 0x15f);
            this.bbiOK.Name = "bbiOK";
            this.bbiOK.Size = new Size(0x68, 0x1b);
            this.bbiOK.TabIndex = 7;
            this.bbiOK.Text = "Đồng \x00dd";
            this.bbiOK.Click += new EventHandler(this.bbiOK_Click);
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.ClientSize = new Size(0x1f9, 390);
            base.Controls.Add(this.btClose);
            base.Controls.Add(this.labelControl1);
            base.Controls.Add(this.gcList);
            base.Controls.Add(this.txtTitle);
            base.Controls.Add(this.bbiOK);
            base.Controls.Add(this.bbiPreview);
            base.FormBorderStyle = FormBorderStyle.FixedSingle;
            base.MaximizeBox = false;
            base.MinimizeBox = false;
            base.Name = "xfmPrintPropertiesSelect";
            base.ShowIcon = false;
            base.ShowInTaskbar = false;
            base.StartPosition = FormStartPosition.CenterScreen;
            this.Text = "T\x00f9y Chọn In";
            this.gcList.EndInit();
            this.gbList.EndInit();
            this.repPosition.EndInit();
            this.txtTitle.Properties.EndInit();
            base.ResumeLayout(false);
        }

        private DataTable LoadFullDataTable()
        {
            DataTable table = this.CreateNullDataTable();
            DataTable table2 = this.LoadSelectDataTable();
            foreach (HRM_EMPLOYEEFields fields in Enum.GetValues(typeof(HRM_EMPLOYEEFields)))
            {
                bool flag = false;
                foreach (DataRow row in table2.Rows)
                {
                    if (fields.ToString() == row["FieldCode"].ToString())
                    {
                        table.Rows.Add(new object[] { true, fields.ToString(), row["FieldName"].ToString(), float.Parse(row["Width"].ToString()), float.Parse(row["Position"].ToString()) });
                        flag = true;
                    }
                }
                if (!flag)
                {
                    table.Rows.Add(new object[] { false, fields.ToString(), Enumerations.GetEnumDescription(fields), 50, 0 });
                }
            }
            return table;
        }

        public DataTable LoadSelectDataTable()
        {
            DataTable table = this.CreateNullDataTable();
            try
            {
                DataSet set = new DataSet();
                set.ReadXml(this.xmlListPath);
                foreach (DataRow row in set.Tables[0].Rows)
                {
                    table.Rows.Add(new object[] { bool.Parse(row[0].ToString()), row[1].ToString(), row[2].ToString(), float.Parse(row[3].ToString()), int.Parse(row[4].ToString()) });
                }
            }
            catch
            {
            }
            return table;
        }

        public string LoadTitle()
        {
            try
            {
                DataSet set = new DataSet();
                set.ReadXml(this.xmlTitlePath);
                return set.Tables[0].Rows[0]["Title"].ToString();
            }
            catch
            {
                return this.Title[this.m_Index];
            }
        }

        private void RaiseAcceptedHandler()
        {
            if (this.Accepted != null)
            {
                this.Accepted(this);
            }
        }

        private void RaisePrintedHandler(DataTable SelectDataTable, string Title)
        {
            if (this.Printed != null)
            {
                this.Printed(this, SelectDataTable, Title);
            }
        }

        private DataTable SaveSelectDataTable()
        {
            DataSet set = new DataSet();
            DataTable table = this.CreateNullDataTable();
            foreach (DataRow row in (this.gcList.DataSource as DataTable).Rows)
            {
                if ((row != null) && Convert.ToBoolean(row["Select"].ToString()))
                {
                    table.Rows.Add(new object[] { Convert.ToBoolean(row["Select"].ToString()), row["FieldCode"].ToString(), row["FieldName"].ToString(), float.Parse(row["Width"].ToString()), int.Parse(row["Position"].ToString()) });
                }
            }
            set.Tables.Add(table);
            try
            {
                set.WriteXml(this.xmlListPath);
            }
            catch
            {
            }
            return table;
        }

        private void SaveTitle()
        {
            DataSet set = new DataSet();
            using (DataTable table = new DataTable())
            {
                table.Columns.Add("Title");
                table.Rows.Add(new object[0]);
                object[] objArray = new object[] { this.txtTitle.Text };
                table.Rows[0][0] = objArray[0];
                set.Tables.Add(table);
            }
            try
            {
                set.WriteXml(this.xmlTitlePath);
            }
            catch
            {
            }
        }

        public delegate void AcceptedHandler(object sender);

        public delegate void PrintedHandler(object sender, DataTable SelectDataTable, string Title);
    }
}

